﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using HotNet;
using HotUI;
namespace SimpleServer{
	public class SimpleServerMgr : MyLuaBehaviour{
		public static SimpleServerMgr Instance;
		public Dictionary<int,DelegateNetData> handleSend = new Dictionary<int, DelegateNetData>();
		public void BindEvent(int command,DelegateNetData callback){
			Debug.LogWarning("add Command:"+command);
			handleSend.Add(command,callback);
		}
		protected override void LoadLuaAssets ()
		{
			base.LoadLuaAssets ();
			luaScriptPath = "lua_ss_server";
			luaScript = GameTool.ResourcesLua(luaScriptPath) as TextAsset;
			InitLua();
		}
		protected override void OnLuaInited ()
		{

			Instance= this;
			base.OnLuaInited ();
			SocketMgr.Instance.onSendToNet  = OnRecv;
		}

		public void OnRecv(NetData data){
			if(handleSend.ContainsKey(data.command)){
				Debug.Log("发往Lua server: cmd"+data.command+" data:"+data.data);
				handleSend[data.command](data);
			}
			else
				Debug.Log ("找不到 :"+data.command+"协议的处理者");
		}

		public void SendToClient(string data){
			var node = JSON.JSONNode.Parse(data);

			SocketMgr.Instance.OnRecvMsg(node["command"].AsInt,data,node["state"].AsInt == 0);
		}

	}
}